using System;
using System.Collections.Generic;
using NBidi;

namespace NBiDi.Extensions
{
    public static class NBiDiExtensions
    {
        public static bool IsTextNeedAlignToRight(string TextToCheckIfNeedsRTL)
        {
            for (int i = 0; i < TextToCheckIfNeedsRTL.Length; i++)
            {
                if (IsRTLChar(TextToCheckIfNeedsRTL[i]))
                    return true;
                if (IsLTRChar(TextToCheckIfNeedsRTL[i]))
                    return false;
            }
            return false;
        }

        public static bool IsRTLChar(char charToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR =
                       BidiCharacterTypeResolver.GetBidiCharacterType(charToCheck);
            return (IsBidiCharTypeRTL(curBidiCharTypeToCheckIfRTLorLTR));
        }


        public static bool IsLTRChar(char charToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR =
                       BidiCharacterTypeResolver.GetBidiCharacterType(charToCheck);
            return (IsBidiCharTypeLTR(curBidiCharTypeToCheckIfRTLorLTR));
        }


        private static bool IsBidiCharTypeLTR(BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR)
        {
            return (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.L)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.LRE)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.LRO);
        }

        private static bool IsBidiCharTypeRTL(BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR)
        {
            return (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.R)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.RLE)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.AL)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.RLO);
        }

        public static bool IsSpacerCharcter(char charcterToCheck)
        {

            return (IsWhitespace(charcterToCheck)
                || IsPunctionChar(charcterToCheck));
        }

        public static bool IsPunctionChar(char charcterToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfWhitespace =
    BidiCharacterTypeResolver.GetBidiCharacterType(charcterToCheck);
            return (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.CS);
            //    return Char.IsPunctuation(charcterToCheck);
        }

        public static bool IsWhitespace(char charcterToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfWhitespace =
                BidiCharacterTypeResolver.GetBidiCharacterType(charcterToCheck);
            return ((curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.WS)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.B)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.S)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.ON))
            ;
        }

        public static bool IsNumberCharcter(char charToCheck)
        {
            return Char.IsDigit(charToCheck);
        }

        public static string[] SplitToParagraphs(string StringToSplit)
        {
            string[] ReturnValues = StringToSplit.Split('\u000A', '\u000D', '\u001C', '\u001E', '\u0085', '\u2029',
                /*Used by blend for breaklines */ '\u2028');

            return ReturnValues;
        }

        public static char GetLTRorRTLStarterCharcterForLine(bool RTL)
        {
            if (RTL)
                return RTLFirstCharMark;
            else
                return LTRFirstCharMark;
        }

        public static char LTRFirstCharMark
        {
            get
            {
                return NBidi.BidiChars.LRE;
            }
        }

        public static char RTLFirstCharMark
        {
            get
            {
                return NBidi.BidiChars.RLE;
            }
        }

        public static bool? GetLTRorRTLStarterCharcterForLineFromLastCharToFirst(string TextToCheckIfNeedsRTL)
        {
            for (int i = TextToCheckIfNeedsRTL.Length - 1; i >= 0; i--)
            {
                if (IsRTLChar(TextToCheckIfNeedsRTL[i]))
                    return true;
                if (IsLTRChar(TextToCheckIfNeedsRTL[i]))
                    return false;
            }
            return null;
        }

        public static string ReverseStringByWhiteSpaceForNumbersOnly(string stringToReverseByWhitespaces)
        {
            char[] whiteSpaceChars =
                new char[] {'\u000C', '\u0020', '\u1680', '\u180E', '\u2000', '\u200A', '\u2028', '\u205F', '\u3000'};

            List<string> seperatedWords = new List<string>(stringToReverseByWhitespaces.Split(whiteSpaceChars));

            seperatedWords.Reverse();

            return String.Join("\u000C", seperatedWords.ToArray());
        }
    }
}